<?php
/**
 * 环信群操作
 */

namespace App\Services\Easemob;

use App\Services\Easemob\UserAuth as EasemobUserAuth;
use Easemob\Auth;
use Easemob\Group;
use Easemob\Http\Http;
use Easemob\User;
use Easemob\UserMetadata;

use App\Models\Member\Member;

use App\Services\Com\UtilService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Groups extends Base
{
    /** 
     * 获取管理员列表
     * @param group_id
     * @throws \Exception
     */
    public static function chatgroupsAdmin(string $group_id = '')
    {
        $auth = EasemobUserAuth::auth();
        $easemobToken = $auth->getEasemobToken();
        $apiUr = $auth->getBaseUri();
        $headers = $auth->headers();
        $uri = $apiUr.'/chatgroups/'.$group_id.'/admin';
        
        $resp = Http::get($uri, $headers);
        if (!$resp->ok()) {
            Log::alert(var_export($resp,true));
        }
        return $resp;
    }

    /** 
     * 获取禁言列表
     * @param group_id
     * @throws \Exception
     */
    public static function chatgroupsMute(string $group_id = '')
    {
        $auth = EasemobUserAuth::auth();
        $easemobToken = $auth->getEasemobToken();
        $apiUr = $auth->getBaseUri();
        $headers = $auth->headers();
        $uri = $apiUr.'/chatgroups/'.$group_id.'/mute';
        
        $resp = Http::get($uri, $headers);
        if (!$resp->ok()) {
            Log::alert(var_export($resp,true));
        }else{
            Log::info(var_export($resp,true));
        }
        return $resp;
    }

    /** 创建公开群组
     * @param $param
     * @throws \Exception
     */
    public static function createPublicGroup(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);
        // 创建并返回组id
        $param['owner_uuid'] = UtilService::memberUuidToImAccount($param['owner_uuid']);
        $group_members       = !empty($param['group_members']) ? $param['group_members'] : null;
        $groupMembers        = null;
        if (!empty($group_members)) {
            $groupMembers = array_map(function ($v) {
                return UtilService::memberUuidToImAccount($v);
            }, $group_members);
        }


        return $group->createPublicGroup($param['owner_uuid'], $param['group_name'], $param['group_desc'], $groupMembers);
    }

    /** 更新群组信息
     * @param $param
     * @throws \Exception
     */
    public static function updateGroup(array $param)
    {
        $auth               = self::getInstance();
        $group              = new Group($auth);
        $param['groupname'] = $param['group_name'];
        unset($param['group_name']);
        return $group->updateGroup($param);
    }

    /** 添加人进群组
     * @param $param
     * @throws \Exception
     */
    public static function addMembersGroup(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);
        // 创建并返回组id
        $groupMembers = array_map(function ($v) {
            return UtilService::memberUuidToImAccount($v);
        }, $param['group_members']);
        return $group->addGroupMembers($param['group_id'], $groupMembers);
    }

    /** 移除群组内人员
     * @param $param
     * @throws \Exception
     */
    public static function removeMembersGroup(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);
        // 创建并返回组id
        $groupMembers = array_map(function ($v) {
            return UtilService::memberUuidToImAccount($v);
        }, $param['group_members']);
        return $group->removeGroupMembers($param['group_id'], $groupMembers);
    }

    /** 添加管理员
     * @param $param
     * @throws \Exception
     */
    public static function addGroupAdmin(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);
        // 添加管理员
        $groupMember = UtilService::memberUuidToImAccount($param['group_member']);
        return $group->addGroupAdmin($param['group_id'], $groupMember);
    }

    /** 移除群管理员
     * @param $param
     * @throws \Exception
     */
    public static function removeGroupAdmin(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);
        // 添加管理员
        $groupMember = UtilService::memberUuidToImAccount($param['group_member']);
        return $group->removeGroupAdmin($param['group_id'], $groupMember);
    }

    /** 获取群详情
     * @param $param
     * @throws \Exception
     */
    public static function getGroup(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);

        return $group->getGroup($param['group_id']);
    }

    /** 转让群
     * @param $param
     * @throws \Exception
     */
    public static function updateGroupOwner(array $param)
    {
        $auth        = self::getInstance();
        $group       = new Group($auth);
        $groupMember = UtilService::memberUuidToImAccount($param['group_member']);
        return $group->updateGroupOwner($param['group_id'], $groupMember);
    }


    /** 解散群
     * @param $param
     * @throws \Exception
     */
    public static function destroyGroup(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);

        return $group->destroyGroup($param['group_id']);
    }

    /** 获取群公告
     * @param $param
     * @throws \Exception
     */
    public static function getGroupAnnouncement(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);

        return $group->getGroupAnnouncement($param['group_id']);
    }

    /** 获取群公告
     * @param $param
     * @throws \Exception
     */
    public static function updateGroupAnnouncement(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);

        return $group->updateGroupAnnouncement($param['group_id'], $param['announcement']);
    }

    /** 获取群成员列表
     * @param $param
     * @throws \Exception
     */
    public static function listGroupMembers(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);

        return $group->listGroupMembers($param['group_id'], $param['page_size'], $param['page']);
    }


    /** 上传群文件
     * @param $param
     * @throws \Exception
     */
    public static function uploadGroupShareFile(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);

        return $group->uploadGroupShareFile($param['group_id'], $param['filename']);
    }

    /** 上传群文件
     * @param $param
     * @throws \Exception
     */
    public static function getGroupShareFiles(array $param)
    {
        $auth  = self::getInstance();
        $group = new Group($auth);

        return $group->getGroupShareFiles($param['group_id'], $param['page_size'], $param['page']);
    }

    /**
     * 群禁言
     * @param array $param
     * @return
     */
    public static function groupMemberMute(array $param = [])
    {
        $auth         = EasemobUserAuth::auth();
        $easemobToken = $auth->getEasemobToken();
        $apiUr        = $auth->getBaseUri();
        $headers      = $auth->headers();
        $uri          = $apiUr . "/chatgroups/{$param['group_id']}/mute";
        
        if($param['chat_status'] == 0){
            $uri .= '/'.UtilService::memberUuidToImAccount($param['member_uuid']);
            $resp = Http::delete($uri, '', $headers);
        }else{
            $body         = [
                'usernames'     => [UtilService::memberUuidToImAccount($param['member_uuid'])],
                'mute_duration' => 3600*24*1000*1000
            ];    
            $resp = Http::post($uri, $body, $headers);
        }

        
        if (!$resp->ok()) {
            Log::alert(var_export($resp, true));
            return false;
        } else {
            return true;
        }
    }

}